package com.nikkerball.calculator;

import com.nikkerball.Computation;

import java.math.BigDecimal;
import java.util.Properties;

/**
 * TODO: Document class
 *
 * @author marius (created on 2010-02-18)
 */
public class Calculation implements Computation {

    // ----- instance fields -----

    private Operator operator;

    private double result;

    private BigDecimal value1;

    private BigDecimal value2;

    // ----- constructors -----

    public Calculation(BigDecimal value1, BigDecimal value2, Operator operator) {
        this.value1 = value1;
        this.value2 = value2;
        this.operator = operator;
        calculateResult(value1, value2, operator);
    }

    // ----- Computation -----

    public String getDescription(Properties labels) {
        StringBuilder description = new StringBuilder();
        description.append(getValue1()).append(" ")
                .append(labels.getProperty(getOperator().getKey(), getOperator().getKey())).append(" ")
                .append(getValue2())
                .append(" = ")
                .append(getResult());
        return description.toString();
    }

    // ----- public methods -----

    public Operator getOperator() {
        return operator;
    }

    public BigDecimal getResult() {
        return BigDecimal.valueOf(result);
    }

    public BigDecimal getValue1() {
        return value1;
    }

    public BigDecimal getValue2() {
        return value2;
    }

    // ----------

    private void calculateResult(BigDecimal value1, BigDecimal value2, Operator operator) {
        switch (operator) {
            case Add:
                result = value1.doubleValue() + value2.doubleValue();
                break;
            case Subtract:
                result = value1.doubleValue() - value2.doubleValue();
                break;
            case Multiply:
                result = value1.doubleValue() * value2.doubleValue();
                break;
            case Devide:
                result = value1.doubleValue() / value2.doubleValue();
                break;
            case Power:
                result = Math.pow(value1.doubleValue(), value2.doubleValue());
                break;
        }
    }

}
